/*
 * Copyright (C) 2013 Baidu Inc. All rights reserved.
 */
package com.baidu.android.bugreport;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemProperties;

public class SendBug {

    private Context mContext;
    
    public SendBug(Context context) {
        mContext = context;
    }


    public void sengBugreport(String bugreportPath) {
        final File bugreport = new File(bugreportPath);

        if (bugreport.exists()) {
            final Uri bugreportUri = Uri.fromFile(bugreport);

            Intent intent = null;
            if (intent == null) {
                intent = getSendMailIntent(bugreportUri);
            }
            try {
                mContext.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    private Intent getSendMailIntent(Uri bugreportUri) {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setType("application/octet-stream");
        intent.putExtra("subject", bugreportUri.getLastPathSegment());
        final StringBuilder sb = new StringBuilder();
        sb.append(SystemProperties.get("ro.build.description"));
        sb.append("\n" +MainActivity.getDeviceInfo(mContext));
        sb.append("\n(Sent from BugReport.BugMailer)");
        intent.putExtra("body", sb.toString());
        
        intent.putExtra(Intent.EXTRA_STREAM, bugreportUri);
        
        return intent;
    }


}
